package threadbase.fiber;

import co.paralleluniverse.fibers.Fiber;

import java.util.concurrent.ExecutionException;

/**
 * 协程样例
 *
 * @author xujian
 * Creation date 2024/2/19 16:12
 * @version 1.0
 */
public class JavaFiber {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        long begin = System.currentTimeMillis();
        int fiberLength = 100000;
        Fiber<Void>[] threads = new Fiber[fiberLength];
        for(int i=0;i<fiberLength;i++){
            threads[i] = new Fiber(() -> {
                calc();
            });
        }

        for (int i = 0; i < fiberLength; i++) {
            threads[i].start();
        }
        for (int i = 0; i < fiberLength; i++) {
            threads[i].join();
        }
        System.out.println("const time = "+(System.currentTimeMillis() - begin));

    }

    //200w次计算
    static void calc() {
        int result = 0;
        for (int i = 0; i < 10000; i++) {
            for (int j = 0; j < 200; j++) {
                result += i;
            }
        }
    }


}
